<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('博客管理','/assets/module/dtree/dtree.css,/assets/module/dtree/font/dtreefont.css')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">板块：</label>
                        <div class="layui-input-inline mr0" style="width:200px;">
                            <ul id="catalogSelect" class="dtree" data-id="-1"></ul>
                        </div>
                    </div>
                    <div class="layui-inline">
                    <label class="layui-form-label w-auto">状态：</label>
                    <div class="layui-input-inline mr0" style="width: 100px;">
                        <select id="postState">
                            <option value=""></option>
                            <option value="top">置顶</option>
                            <option value="support">推荐</option>
                            <option value="state">状态</option>
                        </select>
                    </div>
                </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                </div>
            </div>
            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarOepration">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/blog/" ajax-id="blogId">
            <i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/blog/" value="{{d.blogId}}">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/blog/updateState/" value="{{d.blogId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="data-table-top">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/blog/updateTop/" ajax-state="blogTop" value="{{d.blogId}}" lay-skin="switch" lay-text="置顶|取消"
           {{d.blogTop==1?'checked':''}}/>
</script>
<script type="text/html" id="data-table-support">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/blog/updateSupport/" ajax-state="blogSupport" value="{{d.blogId}}" lay-skin="switch" lay-text="推荐|取消"
           {{d.blogSupport==1?'checked':''}}/>
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin','dtree'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var dtree = layui.dtree;

        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/blog/',
            page : true,
			loading : true,
            toolbar: '#toolbarOepration',
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type: 'checkbox'},
                {field: 'blogTitle',width:300, title: '文章名',templet: function (d) {
                        return '<a href="/blog/'+ d.blogId +'" target="_blank" style="text-decoration: underline">'+d.blogTitle+'</a>';
                    }},
                {field: 'userName',width:100, title: '文章作者',templet: function (d) {
                        return d.author.userName;
                    }},
                {field: 'typeName',width:140, sort: true, title: '博客类型'},
                {field: 'commentCount',width:70,sort:true, title: '回复'},
                {field: 'state', width:100,title: '状态',templet: '#data-table-state'},
                {field: 'blogTop', width:100,title: '置顶',templet: '#data-table-top'},
                {field: 'blogSupport', width:100,title: '推荐',templet: '#data-table-support'},
                {
                    field: 'createTime',width:180, sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#data-table-bar', title: '编辑'}
            ]
            ]
        });
        //头工具栏事件
        table.on('toolbar(data-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    showEditModel();
                    break;
            };
        });
        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }
        });
        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            console.info(dtree.getNowParam(CatalogTree));
            var param = {
                keyword:keyword,
                typeId:$('#typeId').val(),
                state:$('#state').val()
            }
            tableIns.reload( {where: param},'data');
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            B.info("请前往前台修改");
        };
        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            var param = {
            blogId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }
            updateState(obj,param);

        });
        // 修改置顶
        form.on('switch(data-table-top)', function (obj) {
            var param={
                blogId:obj.elem.value,
                postTop:obj.elem.checked ? 1 : 0
            }
            updateState(obj,param);
        });
        // 修改推荐
        form.on('switch(data-table-support)', function (obj) {
            var param={
                blogId:obj.elem.value,
                postSupport:obj.elem.checked ? 1 : 0
            }
            updateState(obj,param);
        });
        function updateState(obj,param){
            B.post({
                url: '/admin/post/updateState',
                loading: true,
                data: param,
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        tableIns.reload({},'data');// 重载表格
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });
        }



    });

</script>
</html>